// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

/**
 * 输入密码界面
 */
cc.Class({
    extends: cc.Component,

    properties: {
        ebPwd:cc.Label,  //输入密码
        ndLists:[cc.Node], //密码
        numberKeysPre: cc.Prefab,  //键盘界面预制
        _code:"",//输入的密码    
        _backState: false,
        _keysOpen: false
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.ebPwd.string = "";
    },

    init: function(bankCode,edMoney,home){
        this._bankCode =bankCode;
        this._edMoney =edMoney;
        this._home =home;
    },

    onTextBegan: function(){ 
        if(!this.node.getChildByName('public_numberKeys_pop')){  //当键盘存在，就不必创建
            this.KeysPre =cc.instantiate(this.numberKeysPre);
            var Callback = function(data){  //键盘输入回调
                this.onTextChanged(data);
            }.bind(this);
            this.KeysPre.getComponent(this.KeysPre.name).init(Callback);
            this.KeysPre.getComponent(this.KeysPre.name).onShow();  //显示键盘界面
            this.node.addChild(this.KeysPre);
        }else{
            this.KeysPre.getComponent(this.KeysPre.name).onShow();  //显示键盘界面   
        }
        this._keysOpen =true;
    },

    //输入密码时
    onTextChanged: function(text) {
        var txt = text;
        if(Utils.isInt(txt) || txt == "")
        {
            if(this._code.length < 6){
                this._code += txt;
            }
            for(var i=0;i<6;i++)
            {
                if(this._code.length>=i)
                    this.ndLists[i].active = true;  
                else
                    this.ndLists[i].active = false;
            }
        }
        else
        {
            if(txt =='-'){
                if(this._code.length > 0){
                    this._code =this._code.slice(0,-1);   //清除输入的密码
                }  
            }else if(txt =='ok'){
                this.KeysPre.getComponent(this.KeysPre.name).onHide();  //隐藏键盘界面   
            }
        }

        for(var i=0;i<6;i++)
        {
            if(this._code.length-1>=i)
                this.ndLists[i].active = true;  
            else
                this.ndLists[i].active = false;
        }
    },

    onTrue: function(){
        if(this._code.length == 6)
        { 
            var recv = function(ret){
                ComponentsUtils.unblock();
                if(ret.Code !== 0)
                {
                    ComponentsUtils.showTips(ret.Msg);
                    cc.error(ret.Msg);
                    this.onBack();
                }
                else
                {
                    window.Notification.emit("USER_useruirefresh","");
                    ComponentsUtils.showTips("提现申请成功！");
                    //申请成功后，(关闭键盘，当前界面，提现界面)回到用户中心界面
                    this._backState =true;
                    this.onClose();
                    this._home.onClose(); 
                }
            }.bind(this);  

            var amont = LotteryUtils.moneytoClient(parseFloat(this._edMoney))
            var data = {
                Token:User.getLoginToken(),
                UserCode:User.getUserCode(),
                BankCode:this._bankCode,
                Amount:amont,
                PayPwd:parseInt(this._code)
            }
            CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.WITHDRAWALS, data, recv.bind(this),"POST");  
            ComponentsUtils.block();
        }
    },

    //点击其他位置，关闭键盘
    onCloseKeys: function(){
        if(!this._keysOpen){
            return;
        }
        if(this.KeysPre){
            this.KeysPre.getComponent(this.KeysPre.name).onHide();  //隐藏键盘界面
            this._keysOpen =false;
        }
    },

    onBack: function(){
        this.ebPwd.string = "";
        this._code = "";
        for(var i=0;i<this.ndLists.length;i++)
        {
            this.ndLists[i].active = false;
        }    
    },

    onClose: function(){
        this.onBack();
        if(this._backState){  //输入密码成功后，按确定按钮
            this.KeysPre.getComponent(this.KeysPre.name).onClose();  //清除键盘界面
        }
        if(this.KeysPre){  //若键盘界面是开启的
            this.KeysPre.getComponent(this.KeysPre.name).onClose();  //清除键盘界面 
        }  
        this.node.getComponent("Page").backAndRemove();
    },


    // update (dt) {},
});
